package com.gt.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.gt.constant.MessageConstant;
import com.gt.constant.StatusConstant;
import com.gt.context.BaseContext;
import com.gt.exception.BackFailedException;
import com.gt.mapper.BookMapper;
import com.gt.mapper.BorrowMapper;
import com.gt.pojo.PageResult;
import com.gt.pojo.dto.BorrowConfirmDTO;
import com.gt.pojo.dto.BorrowPageQueryDTO;
import com.gt.pojo.entity.Book;
import com.gt.pojo.entity.Borrow;
import com.gt.pojo.vo.BorrowVO;
import com.gt.service.BorrowService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class BorrowServiceImpl implements BorrowService {
    @Autowired
    private BorrowMapper borrowMapper;
    @Autowired
    private BookMapper bookMapper;

    @Override
    public PageResult conditionSearch(BorrowPageQueryDTO borrowPageQueryDTO) {
        PageHelper.startPage(borrowPageQueryDTO.getPage(), borrowPageQueryDTO.getPageSize());
        Page<BorrowVO> page = borrowMapper.pageQuery(borrowPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void confirm(BorrowConfirmDTO borrowConfirmDTO) {
           Borrow borrow = borrowMapper.selectById(borrowConfirmDTO.getId());
           Integer status = borrow.getStatus();
           if(status == StatusConstant.BORROW_ERROR){
               borrow.setStatus(StatusConstant.RETURNED);
               borrow.setEndTime(LocalDateTime.now());
           }
           if (status == StatusConstant.RENEWAL_UNDER_REVIEW){
               borrow.setStatus(StatusConstant.RENEWING);
               LocalDateTime endTime = borrow.getEndTime().plusDays(15);
               borrow.setEndTime(endTime);
           }
           if (status == StatusConstant.RETURNING_FOR_APPROVAL)
           {
               borrow.setStatus(StatusConstant.RETURNED);
               borrow.setEndTime(LocalDateTime.now());
               Book book = bookMapper.getById(borrow.getBookId());
               if (book != null){
                   book.setNumber(book.getNumber()+1);
                   book.setUpdateTime(LocalDateTime.now());
                   bookMapper.update(book);
               }
           }
           borrowMapper.update(borrow);
       }

    @Override
    public void back(BorrowConfirmDTO borrowConfirmDTO) {
        Borrow borrow = borrowMapper.selectById(borrowConfirmDTO.getId());
        if(borrow.getStatus() == StatusConstant.BORROW_ERROR){
            throw new BackFailedException(MessageConstant.BACK_FAILED);
        }
        if (borrow.getStatus() ==StatusConstant.BORROWING ||borrow.getStatus() == StatusConstant.RENEWING){
            Borrow borrow1 = Borrow.builder()
                    .id(borrowConfirmDTO.getId())
                    .status(StatusConstant.RETURNING_FOR_APPROVAL)
                    //.endTime(LocalDateTime.now())
                    .build();
       /*     Book book = bookMapper.getById(borrow.getBookId());
            if (book != null){
                book.setNumber(book.getNumber()+1);
                book.setUpdateTime(LocalDateTime.now());
                bookMapper.update(book);
            }*/
            borrowMapper.update(borrow1);
        }


    }

    @Override
    public void renew(BorrowConfirmDTO borrowConfirmDTO) {
        Borrow borrow = borrowMapper.selectById(borrowConfirmDTO.getId());
        if (borrow.getStatus() == StatusConstant.BORROWING || borrow.getStatus() == StatusConstant.RENEWING){
            Borrow borrow1 = Borrow.builder()
                    .status(StatusConstant.RENEWAL_UNDER_REVIEW)
                    .id(borrowConfirmDTO.getId())
                    .build();
            borrowMapper.update(borrow1);
        }
    }

    @Override
    public PageResult Search(BorrowPageQueryDTO borrowPageQueryDTO) {
        PageHelper.startPage(borrowPageQueryDTO.getPage(), borrowPageQueryDTO.getPageSize());
        borrowPageQueryDTO.setUserId(BaseContext.getCurrentId());
        Page<BorrowVO> page = borrowMapper.query(borrowPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void deleteById(Long id) {
        borrowMapper.deleteById(id);
    }


}
